﻿/*
 * Copyright 2008 Lucifer Lu
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;

namespace SilverChart.Components {
    public abstract class AxisGridBaseComponent : CompositeChartWidget {
        private Path majorGrid;
        private GeometryGroup majorGroup;

        private Path minorGrid;
        private GeometryGroup minorGroup;

        public AxisGridBaseComponent(CartesianCoordinateAxis axis) {
            Axis = axis;

            WidgetsManager.AddComponent(this);
        }

        public override void InitializeWidgets() {
            if (Axis.AxisConfig.MajorAxisGrid) {
                majorGrid = Axis.AxisConfig.MajorGridLine;
                majorGroup = new GeometryGroup();
                majorGrid.Data = majorGroup;
                Children.Add(majorGrid);
            }

            if (Axis.AxisConfig.MinorAxisGrid) {
                minorGrid = Axis.AxisConfig.MinorGridLine;
                minorGroup = new GeometryGroup();
                minorGrid.Data = minorGroup;
                Children.Add(minorGrid);
            }
        }

        public CartesianCoordinateAxis Axis { get; private set; }

        protected override Size MeasureOverride(Size availableSize) {
            Size aSize = new Size(Math.Min(availableSize.Width, 10000),
                Math.Min(availableSize.Height, 10000));
            return aSize;
        }

        protected override Size ArrangeOverride(Size finalSize) {
            int tickCount = 1;
            majorGroup.Children.Clear();
            minorGroup.Children.Clear();
            for (Double value = Axis.MinValue + Axis.ValuePerTick;
                Math.Round(value, 1) <= Axis.MaxValue; value += Axis.ValuePerTick) {
                Double valueInAxis = Axis.MappingValueToAxis(value);
                LineGeometry gridLine = new LineGeometry();
                ProcessGridLine(valueInAxis, finalSize, gridLine);
                if (Axis.AxisConfig.MajorAxisGrid && Axis.MajorTick(tickCount)) {
                    majorGroup.Children.Add(gridLine);
                }
                else if (Axis.AxisConfig.MinorAxisGrid) {
                    minorGroup.Children.Add(gridLine);
                }
                tickCount++;
            }
            return finalSize;
        }

        protected abstract void ProcessGridLine(Double valueInAxis,
            Size drawSize, LineGeometry gridLine);
    }
}
